<?php require_once('../Connections/aciitu.php'); ?>
<?php
// Load the common classes
require_once('../includes/common/KT_common.php');

// Load the tNG classes
require_once('../includes/tng/tNG.inc.php');

// Load the KT_back class
require_once('../includes/nxt/KT_back.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("../");

// Make unified connection variable
$conn_aciitu = new KT_connection($aciitu, $database_aciitu);

// Start trigger
$formValidation = new tNG_FormValidation();
$formValidation->addField("nome", true, "text", "", "", "", "");
$formValidation->addField("estado_id", true, "numeric", "", "", "", "");
$tNGs->prepareValidation($formValidation);
// End trigger

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_aciitu, $aciitu);
$query_RsEstado = "SELECT * FROM estado ORDER BY nome ASC";
$RsEstado = mysql_query($query_RsEstado, $aciitu) or die(mysql_error());
$row_RsEstado = mysql_fetch_assoc($RsEstado);
$totalRows_RsEstado = mysql_num_rows($RsEstado);

// Make an insert transaction instance
$ins_cidade = new tNG_multipleInsert($conn_aciitu);
$tNGs->addTransaction($ins_cidade);
// Register triggers
$ins_cidade->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1");
$ins_cidade->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$ins_cidade->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$ins_cidade->setTable("cidade");
$ins_cidade->addColumn("nome", "STRING_TYPE", "POST", "nome");
$ins_cidade->addColumn("estado_id", "NUMERIC_TYPE", "POST", "estado_id");
$ins_cidade->setPrimaryKey("id", "NUMERIC_TYPE");

// Make an update transaction instance
$upd_cidade = new tNG_multipleUpdate($conn_aciitu);
$tNGs->addTransaction($upd_cidade);
// Register triggers
$upd_cidade->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Update1");
$upd_cidade->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$upd_cidade->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$upd_cidade->setTable("cidade");
$upd_cidade->addColumn("nome", "STRING_TYPE", "POST", "nome");
$upd_cidade->addColumn("estado_id", "NUMERIC_TYPE", "POST", "estado_id");
$upd_cidade->setPrimaryKey("id", "NUMERIC_TYPE", "GET", "id");

// Make an instance of the transaction object
$del_cidade = new tNG_multipleDelete($conn_aciitu);
$tNGs->addTransaction($del_cidade);
// Register triggers
$del_cidade->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Delete1");
$del_cidade->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$del_cidade->setTable("cidade");
$del_cidade->setPrimaryKey("id", "NUMERIC_TYPE", "GET", "id");

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rscidade = $tNGs->getRecordset("cidade");
$row_rscidade = mysql_fetch_assoc($rscidade);
$totalRows_rscidade = mysql_num_rows($rscidade);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
-->
</style>

<!--lightbox-->
<script type="text/javascript" src="../ferramentas/lightbox/js/prototype.js"></script>
<script type="text/javascript" src="../ferramentas/lightbox/js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="../ferramentas/lightbox/js/lightbox.js"></script>
<link rel="stylesheet" href="../ferramentas/lightbox/css/lightbox.css" type="text/css" media="screen" />
<!--fim lightbox-->

<!-- TinyMCE 
<script type="text/javascript" src="../ferramentas/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
		//Tira a TAG inicial <p>
		forced_root_block: false,
        // General options
		mode : "textareas",
		theme : "advanced",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
</script>
-->

<link href="../css/css_admin.css" rel="stylesheet" type="text/css" />
<link href="../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="../includes/common/js/base.js" type="text/javascript"></script>
<script src="../includes/common/js/utility.js" type="text/javascript"></script>
<script src="../includes/skins/style.js" type="text/javascript"></script>
<?php echo $tNGs->displayValidationRules();?>
<script src="../includes/nxt/scripts/form.js" type="text/javascript"></script>
<script src="../includes/nxt/scripts/form.js.php" type="text/javascript"></script>
<script type="text/javascript">
$NXT_FORM_SETTINGS = {
  duplicate_buttons: false,
  show_as_grid: true,
  merge_down_value: true
}
</script>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="texto_barra">
  <tr>
    <td>
      <?php
	echo $tNGs->getErrorMsg();
?>
      <div class="KT_tng">
        <h1>
          <?php 
// Show IF Conditional region1 
if (@$_GET['id'] == "") {
?>
            <?php echo NXT_getResource("Insert_FH"); ?>
            <?php 
// else Conditional region1
} else { ?>
            <?php echo NXT_getResource("Update_FH"); ?>
            <?php } 
// endif Conditional region1
?>
          Cidade </h1>
        <div class="KT_tngform">
          <form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
            <?php $cnt1 = 0; ?>
            <?php do { ?>
              <?php $cnt1++; ?>
              <?php 
// Show IF Conditional region1 
if (@$totalRows_rscidade > 1) {
?>
                <h2><?php echo NXT_getResource("Record_FH"); ?> <?php echo $cnt1; ?></h2>
                <?php } 
// endif Conditional region1
?>
              <table cellpadding="2" cellspacing="0" class="KT_tngtable">
                <tr>
                  <td class="KT_th"><label for="nome_<?php echo $cnt1; ?>">Nome:</label></td>
                  <td><input type="text" name="nome_<?php echo $cnt1; ?>" id="nome_<?php echo $cnt1; ?>" value="<?php echo KT_escapeAttribute($row_rscidade['nome']); ?>" size="50" maxlength="100" />
                    <?php echo $tNGs->displayFieldHint("nome");?> <?php echo $tNGs->displayFieldError("cidade", "nome", $cnt1); ?></td>
                </tr>
                <tr>
                  <td class="KT_th"><label for="estado_id_<?php echo $cnt1; ?>">Estado:</label></td>
                  <td><select name="estado_id_<?php echo $cnt1; ?>" id="estado_id_<?php echo $cnt1; ?>">
                    <option value=""><?php echo NXT_getResource("Select one..."); ?></option>
                    <?php 
do {  
?>
                    <option value="<?php echo $row_RsEstado['id']?>"<?php if (!(strcmp($row_RsEstado['id'], $row_rscidade['estado_id']))) {echo "SELECTED";} ?>><?php echo $row_RsEstado['nome']?></option>
                    <?php
} while ($row_RsEstado = mysql_fetch_assoc($RsEstado));
  $rows = mysql_num_rows($RsEstado);
  if($rows > 0) {
      mysql_data_seek($RsEstado, 0);
	  $row_RsEstado = mysql_fetch_assoc($RsEstado);
  }
?>
                  </select>
                    <?php echo $tNGs->displayFieldError("cidade", "estado_id", $cnt1); ?></td>
                </tr>
              </table>
              <input type="hidden" name="kt_pk_cidade_<?php echo $cnt1; ?>" class="id_field" value="<?php echo KT_escapeAttribute($row_rscidade['kt_pk_cidade']); ?>" />
              <?php } while ($row_rscidade = mysql_fetch_assoc($rscidade)); ?>
            <div class="KT_bottombuttons">
              <div>
                <?php 
      // Show IF Conditional region1
      if (@$_GET['id'] == "") {
      ?>
                  <input type="submit" name="KT_Insert1" id="KT_Insert1" value="<?php echo NXT_getResource("Insert_FB"); ?>" />
                  <?php 
      // else Conditional region1
      } else { ?>
                  <input type="submit" name="KT_Update1" value="<?php echo NXT_getResource("Update_FB"); ?>" />
                  <input type="submit" name="KT_Delete1" value="<?php echo NXT_getResource("Delete_FB"); ?>" onclick="return confirm('<?php echo NXT_getResource("Are you sure?"); ?>');" />
                  <?php }
      // endif Conditional region1
      ?>
                <input type="button" name="KT_Cancel1" value="<?php echo NXT_getResource("Cancel_FB"); ?>" onclick="return UNI_navigateCancel(event, '../includes/nxt/back.php')" />
              </div>
            </div>
          </form>
        </div>
        <br class="clearfixplain" />
      </div>
    </td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($RsEstado);
?>
